#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# icon_theme.py
#
#  Copyright (c) 2006-2007 INdT (Instituto Nokia de Technologia)
#                2006-2007 Kenneth Rohde Christiansen
#
#  Author: Kenneth Rohde Christiansen <kenneth.christiansen@gmail.com>
#
#  This program is free software; you can redistribute it and/or 
#  modify it under the terms of the GNU General Public License as 
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA

import gtk
import os
import gobject
from debug import debug

my_instance = None

def icon_theme_get_default():
    global my_instance
    if my_instance is None: my_instance = IconThemeFactory()

    return my_instance.get_icon_theme()

class IconThemeFactory:

    def __init__(self):
        if True:
            self.icon_theme = gtk.icon_theme_get_default()
            self.icon_theme.rescan_if_needed()
            #self.icon_theme.list_icons()
        else:
            self.icon_theme = FakeIconTheme()
            self.icon_theme.append_search_path(r'../data/icons')

    def get_icon_theme(self):
        return self.icon_theme

class FakeIconTheme:

    def __init__(self):
        self.paths = []

    def list_icons(self):
        debug(0, "Not implemented yet")

    def append_search_path(self, path):
        self.paths.append(path)

    def load_icon(self, name, size, flags):
        for path in self.paths:
            if size >= 48:
                filename = os.path.join(path, "scalable", name + ".svg")
            else:
                filename = os.path.join(path, str(size) + "x" + str(size), name + ".png")

            if os.path.exists(filename):
                try:
                    return gtk.gdk.pixbuf_new_from_file_at_size(filename.replace("\\", "/"), size, size)
                except gobject.GError, e:
                    if size >= 48:
                        filename = os.path.join(path, str(size) + "x" + str(size), name + ".png")
                        return gtk.gdk.pixbuf_new_from_file(filename.replace("\\", "/"))
                    else:
                        raise e
            else: 
                debug(0, "WARNING: failed opening " + filename)
